// const { testCont, userInfo } = storeToRefs(user)
import { saveStorage, loadStorage } from '@/utils/cache'
window.receiveAndroidUserInfo = async params => {
  saveStorage('userInfo', params)
}
function checkSystem(): string {
  var u = window.navigator.userAgent;
  var isAndroid = u.indexOf("mj_flutter") > -1 || u.indexOf("Linux") > -1; //g
  var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  if (isAndroid) {
    return "android";
  }
  if (isIOS) {
    return "ios";
  }
  return ''
}

export const system = checkSystem();

let ua: string
ua = navigator.userAgent;
const p: string = navigator.platform;
let Common: {
  system: string,
  os: { plus: boolean, wechat: boolean, android: boolean, iphone: boolean, ipad: boolean, mqq: boolean, wechat_pc: boolean, pc: boolean },
  brige(funName: string, param?: { [propName: string]: any }): string,
  getBrige(name: string): string,
  install(app: any): void
}

Common = {
  system,
  os: {
    plus: ua.match(/Html5Plus/i) ? true : false,
    wechat: ua.match(/(MicroMessenger)\/([\d.]+)/i) ? true : false,
    // wechatxcx: ua.match(/(miniprogramhtmlwebview)/i) ? true : false,
    android: ua.match(/(Android);?[\s/]+([\d.]+)?/) ? true : false,
    iphone: ua.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false,
    ipad: ua.match(/(iPad).*OS\s([\d_]+)/) ? true : false,
    mqq: ua.match(/(qq)\/([\d.]+)/i) ? true : false,
    wechat_pc: Boolean(ua.match(/(MicroMessenger)\/([\d.]+)/i)) && (p.indexOf("Win") == 0 || p.indexOf("Mac") == 0),
    pc: !/Android|webOS|iPhone|iPod|BlackBerry/i.test(ua),
  },
  brige: (funName, param = {}) => {
    if (system === "android") {
      //安卓协商传json字符串
      MjAndroid.postMessage(JSON.stringify({
        method: funName,
      }));
      // return Object.keys(param).length ? window.Android[funName](JSON.stringify(param)) : window.Android[funName]();
    } else if (system === "ios" && window.webkit.messageHandlers) {
      console.log(funName, window.webkit.messageHandlers[funName])
      return window.webkit.messageHandlers[funName]["postMessage"](param);
    } else {
      return Promise.reject("请在客户端打开");
    }
  },
  getBrige: name => {
    try {
        return window.js_getUserToken || {};

      if (system === "android") {
        // 后来新增全局方法返回对象getAppInfo 需兼容旧版本旧版本没有getAppInfo方法 映射类型需要存在 兼容
        // console.log(window.Android.getAppInfo, 'getAppInfo方法')
        // console.log(window.Android.getAppInfo(), 'getAppInfo方法返回的数据')
        // return JSON.parse(window.Android.getAppInfo())[name]
        saveStorage('userInfo', window.js_getUserToken)
        console.log(window.js_getUserToken, '安卓window.js_getUserTokenwindow.js_getUserTokenwindow.js_getUserToken')
        MjAndroid.postMessage(JSON.stringify({
          method: 'getAppInfo',
          callbackFunc: "receiveAndroidUserInfo",
        }));
      } else if (system === "ios" && window.js_getUserToken) {
        // ios数据会全部塞到js_getUserToken对象里面
        console.log(window.js_getUserToken)
        // return window.js_getUserToken[name];
        saveStorage('userInfo', window.js_getUserToken)
      }
    } catch (error) {
      return {};
    }
  },
  install: app => {
    app.config.globalProperties.os = Common.os; // 封装公共调取App的方法
    app.config.globalProperties.system = checkSystem(); // 封装公共调取App的方法
    app.config.globalProperties.brige = Common.brige; // 封装公共调取App的方法
    app.config.globalProperties.getBrige = Common.getBrige; // 封装公共获取App数据方法
  }
};

export default Common;

